var _oDataField;


function AddDataValues(s)
{
	if (s)
	{
		s = s.replace("{!", "");
		s = s.replace("}", "");

		var a = s.split(";");

		var idx;
		var oNode;
		var sEntLogName;
		var sEntDispName;
		var sAttrLogName;
		var sAttrDispName;
		var sDataType;

		for (i = 0; i < a.length - 1; i++)
		{
			s = a[i];

			idx	= s.indexOf(":");

			sEntLogName		= s.substring(0, idx);
			sAttrLogName	= s.substring(idx + 1).replace("/@name", "");

			oNode = _oXml.selectSingleNode("/entities/entity[@name = '" + sEntLogName + "']");

			sEntDispName	= oNode.getAttribute("desc");

			oNode = oNode.selectSingleNode("fields/field[@name = '" + sAttrLogName + "']");

			sAttrDispName	= oNode.getAttribute("desc");
			sDataType		= oNode.getAttribute("datatype");

			AddRow(new ValueObj(sEntLogName, sEntDispName, sAttrLogName, sAttrDispName, sDataType));
		}

		crmDialog.DefaultText.value = a[i];
	}
}


function SelectRow()
{
	UnSelectRow();

	var o = event.srcElement;

	while (o.tagName != "TR")
	{
		o = o.parentElement;
	}

	o.style.backgroundColor = "#adc3e7";
	
	_oDataField = o;
}


function UnSelectRow()
{
	if (_oDataField)
	{
		var oRow = _oDataField;

		oRow.style.backgroundColor = "#ffffff";
	}
}


function MoveUp()
{
	if (_oDataField)
	{
		var oRow = _oDataField;

		var iIndex = oRow.rowIndex;

		if (iIndex > 1)
		{
			oRow.swapNode(oRow.parentElement.rows[iIndex - 1]);
		}
	}
}


function MoveDown()
{
	if (_oDataField)
	{
		var oRow = _oDataField;

		var iIndex = oRow.rowIndex;

		if (iIndex < (oRow.parentElement.rows.length - 1))
		{
			oRow.swapNode(oRow.parentElement.rows[iIndex + 1]);
		}
	}
}


function Delete()
{
	if (_oDataField)
	{
		var oRow = _oDataField;

		if (confirm("Do you really want to delete this value?"))
		{
			oRow.parentElement.deleteRow(oRow.rowIndex);
		}

		_oDataField = null;
	}
}


function Add()
{
	var oArgs = new Object();

	oArgs.Xml	= _oXml;
	oArgs.Ent	= "systemuser";
	oArgs.Attr	= null;

	var oValueObj = openStdDlg("/Tools/EmailTemplateEditor/Dialogs/value.aspx?mode=add", oArgs, 250, 220);

	if (oValueObj)
	{
		AddRow(oValueObj);
	}
}


function Edit()
{
	if (_oDataField)
	{
		var oRow	= _oDataField;

		var s	= oRow.value;
		var idx	= s.indexOf(":");

		var oArgs = new Object();

		oArgs.Xml	= _oXml;
		oArgs.Ent	= s.substring(0, idx);
		oArgs.Attr	= s.substring(idx + 1).replace("/@name", "");

		var oValueObj = openStdDlg("/Tools/EmailTemplateEditor/Dialogs/value.aspx?mode=edit", oArgs, 250, 220);

		if (oValueObj)
		{
			PersistRow(oRow, oValueObj);
		}
	}
}


function AddRow(oValueObj)
{
	var oRow = DataFields.firstChild.insertRow();
	
	var oCell = oRow.insertCell();
	
	oCell.noWrap	= true;
	oCell.className	= "hand";

	oRow.attachEvent("onclick", SelectRow);
	oRow.attachEvent("ondblclick", Edit);
	
	PersistRow(oRow, oValueObj);

	oRow.click();
}


function PersistRow(oRow, oValueObj)
{
	var sNameAttrib = "";

	switch (oValueObj.DataType)
	{
		case "lookup":
		case "customer":
		case "owner":
		case "picklist":
		case "boolean":
		case "state":
		case "status":

			sNameAttrib = "/@name";
			
			break;
	}

	oRow.value = oValueObj.EntLogName + ":" + oValueObj.AttrLogName + sNameAttrib;
	oRow.cells[0].innerText	= oValueObj.EntDispName + " : " + oValueObj.AttrDispName;
}